package com.lovo.invoicing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.invoicing.bean.PageBean;
import com.lovo.invoicing.bean.UserBean;
import com.lovo.invoicing.bean.UserRole;
import com.lovo.invoicing.mapper.UserMapper;
import com.lovo.invoicing.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserBean> implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Override
    @Transactional
    public void addUser(String userName,String realName,int[] roleIds) {
        UserBean userBean = new UserBean();
        userBean.setUserName(userName);
        userBean.setRealName(realName);
        baseMapper.insert(userBean);
        List<UserRole> list = new ArrayList<>();
        for (int roleId : roleIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userBean.getUserId());
            userRole.setRoleId(roleId);
            list.add(userRole);
        }
        userMapper.insertUserRole(list);
    }

    @Override
    @Transactional
    public void deleteUser(int userId) {
        userMapper.deleteUserRole(userId);
        userMapper.deleteById(userId);
    }

    //修改用户权限
    @Override
    @Transactional
    public void updateUser(int userId, int[] roleIds) {
        userMapper.deleteUserRole(userId);
        List<UserRole> list = new ArrayList<>();
        for (int roleId : roleIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            list.add(userRole);
        }
        userMapper.insertUserRole(list);
    }

    @Override
    public IPage<UserBean> queryUsersByPage(PageBean<UserBean> pageBean) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if (pageBean.getT().getUserName()!=null && !"".equals(pageBean.getT().getUserName())){
            qw.like("user_name",pageBean.getT().getUserName());
        }
        return baseMapper.selectPage(new Page<>(pageBean.getPageNo(), pageBean.getPageSize()),qw);
    }

    @Override
    public UserBean queryUser(UserBean userBean) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("user_name",userBean.getUserName());
        qw.eq("pass_word",userBean.getPassWord());
        return baseMapper.selectOne(qw);
    }

    @Override
    public List<Integer> queryLimitsByUserId(int userId) {
        List<Integer> roleIds = userMapper.selectRoleIdsByUserId(userId);
        return userMapper.selectLimitByRoleIds(roleIds);
    }

    @Override
    public String queryPassWordById(int userId) {
        return baseMapper.selectById(userId).getPassWord();
    }

    @Override
    @Transactional
    public void updatePassWord(UserBean userBean) {
        UserBean u = baseMapper.selectById(userBean.getUserId());
        userBean.setUserName(u.getUserName());
        baseMapper.updateById(userBean);
    }

    @Override
    public UserBean queryUserByPhoneNumber(String phoneNumber) {
        QueryWrapper<UserBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone_number",phoneNumber);
        return baseMapper.selectOne(queryWrapper);
    }

}
